package xworker.gluon.glisten.control;

import com.gluonhq.charm.glisten.control.NavigationDrawer;
import javafx.geometry.Side;
import javafx.scene.Node;
import org.xmeta.ActionContext;
import org.xmeta.Thing;
import xworker.javafx.beans.property.PropertyFactory;
import xworker.javafx.control.ControlActions;
import xworker.javafx.util.JavaFXUtils;

public class NavigationDrawerActions {
    static{
        PropertyFactory.regist(NavigationDrawer.class, "sideProperty", o -> {
            NavigationDrawer obj = (NavigationDrawer) o;
            return obj.sideProperty();
        });
        PropertyFactory.regist(NavigationDrawer.class, "headerProperty", o -> {
            NavigationDrawer obj = (NavigationDrawer) o;
            return obj.headerProperty();
        });
        PropertyFactory.regist(NavigationDrawer.class, "footerProperty", o -> {
            NavigationDrawer obj = (NavigationDrawer) o;
            return obj.footerProperty();
        });
    }

    public static void init(NavigationDrawer node, Thing thing, ActionContext actionContext) {
        ControlActions.init(node, thing, actionContext);

        Node footer = JavaFXUtils.getObject(thing, "footer", actionContext);
        if (footer != null) {
            node.setFooter(footer);
        }

        Node header = JavaFXUtils.getObject(thing, "header", actionContext);
        if (header != null) {
            node.setHeader(header);
        }

        if(thing.valueExists("side")){
            String side = thing.getString("side");
            node.setSide(Side.valueOf(side));
        }

    }

    public static NavigationDrawer create(ActionContext actionContext) {
        Thing self = actionContext.getObject("self");
        NavigationDrawer node = new NavigationDrawer();
        init(node, self, actionContext);

        actionContext.peek().put("parent", node);
        for (Thing child : self.getChilds()) {
            child.doAction("create", actionContext);
        }

        actionContext.g().put(self.getMetadata().getName(), node);
        return node;
    }

    public static void createFooter(ActionContext actionContext){
        Thing self = actionContext.getObject("self");
        NavigationDrawer parent = actionContext.getObject("parent");
        for(Thing child : self.getChilds()){
            Object obj = child.doAction("create", actionContext);
            if(obj instanceof Node){
                parent.setFooter((Node) obj);
                break;
            }
        }
    }

    public static void createHeader(ActionContext actionContext){
        Thing self = actionContext.getObject("self");
        NavigationDrawer parent = actionContext.getObject("parent");
        for(Thing child : self.getChilds()){
            Object obj = child.doAction("create", actionContext);
            if(obj instanceof Node){
                parent.setHeader((Node) obj);
                break;
            }
        }
    }
}